package com.smedi.ismedi.notification.core.domain.service.impl;

import com.smedi.ismedi.notification.core.domain.aggregatemodel.MailTemplate;
import com.smedi.ismedi.notification.core.domain.aggregatemodel.SmsTemplate;
import com.smedi.ismedi.notification.core.domain.repository.MailTemplateRepository;
import com.smedi.ismedi.notification.core.domain.service.MailTemplateDomainService;
import com.smedi.ismedi.notification.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.notification.core.infrastructure.enumerate.SmsTemplateStatusEnum;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.regex.Pattern;

/**
 * @className: MailTemplateDomainServiceImpl
 * @Description: TODO
 * @version: v1.0.0
 * @author: wzx
 * @date: 2022/11/23 13:42
 */

@Slf4j
@Service
@AllArgsConstructor
public class MailTemplateDomainServiceImpl implements MailTemplateDomainService {

    private MailTemplateRepository mailTemplateRepository;

    @Override
    public MailTemplate createTemplate(MailTemplate mailTemplate) {
        mailTemplate.setParameters(getTemplateParameters(mailTemplate.getTemplateContent()));// 拆分参数链接，将参数定位出来，也为未来拼接参数方便
        return mailTemplateRepository.doInsert(mailTemplate);
    }

    @Override
    public MailTemplate updateTemplate(MailTemplate mailTemplate) {
        MailTemplate basic;
        if (mailTemplate.getId() != null) {
            basic = mailTemplateRepository.selectByMailTemplateId(mailTemplate.getId());
            if (basic == null) {
                throw LogicException.raise(CustomError.MAIL_TEMPLATE_NOT_FOUND);
            }
        } else {
            throw LogicException.raise(CustomError.MAIL_TEMPLATE_ID_MISS);
        }

        // 腾讯模板id不允许请求传入
        basic.setParameters(getTemplateParameters(mailTemplate.getTemplateContent()))
                .setRemark(mailTemplate.getRemark())
                .setTemplateContent(mailTemplate.getTemplateContent())
                .setTemplateName(mailTemplate.getTemplateName());

        return mailTemplateRepository.doUpdate(basic);
    }

    @Override
    public void deleteTemplate(Long mailTemplateId) {
        mailTemplateRepository.doDelete(mailTemplateId);
    }

    @Override
    public MailTemplate queryTemplateByMailTemplateId(Long mailTemplateId) {
        return mailTemplateRepository.selectByMailTemplateId(mailTemplateId);
    }

    @Override
    public MailTemplate queryTemplateByMailTemplateName(String mailTemplateName) {
        return mailTemplateRepository.selectByMailTemplateName(mailTemplateName);
    }

    /**
     * 获取参数个数
     *
     * @param content
     * @return
     */
    private int getTemplateParameters(String content) {
        String words = content;
        int i = 0;
        while (words.contains("{")) {
            words = words.substring(words.indexOf("{") + 1);
            String text = words.substring(0, words.indexOf("}"));
            boolean error = false;
            if (isNumeric(text)) {
                int a = Integer.valueOf(text);
                if (a <= 0) {
                    error = false;
                }
                if (a != i) {
                    error = false;
                }
            } else {
                error = false;
            }
            if (error) throw LogicException.raise(CustomError.TEMPLATE_PARAMETER_ERROR);
            i++;
        }
        return i;
    }

    /**
     * 校验文本是否为数字
     *
     * @param str
     * @return
     */
    private boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }
}